<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Core_Gateway_MemberAccess extends RedCross_Gateway_GatewayAbstract
{
    
    public function __construct()
    {
        $this->_class = 'MemberAccess';
        $this->_modelPath = 'RedCross_Gateway_Base';
    }

    public function revoke($memberId, $accessId) {
        $date = new RedCross_Date();
        /*$query = Doctrine_Query::create()
            ->update($this->getModelClass())
            ->set ('lost_access_date', '?', $date->export())
            ->where('member_id = ?', $memberId)
            ->andWhere('access_id = ?', $accessId);*/
        //return $query->execute();
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass())
            ->where('member_id = ?', $memberId)
            ->andWhere('access_id = ?', $accessId);
        $record = $query->fetchOne();
        $record->lost_access_date = $date->export();
        $record->save();
        return $record;
    }

    public function updateRecord($memberId, $accessId) {
        $date = new RedCross_Date();
        $query = Doctrine_Query::create()
            ->update($this->getModelClass())
            ->set('lost_access_date', 'NULL')
            ->set('gained_access_date', '?', $date->export())
            ->where('member_id = ?', $memberId)
            ->andWhere('access_id = ?', $accessId);
        return $query->execute();
    }

    public function checkIfExists($memberId, $accessId) {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_MemberAccess ma')
            ->where('ma.member_id = ?', $memberId)
            ->andWhere('ma.access_id = ?', $accessId);
        return $query->execute();
    }
}
